package mop.gui.resource;

import java.util.List;
import mop.controller.CreateResourceController;
import mop.gui.GUI;
import mop.model.resource.ResourceTypeInterface;

/**
 * CreateResourceDialog
 * 
 * @author  Laurens
 */
public class CreateResourceDialog extends javax.swing.JDialog
{
    /**
     * Constructor.
     * 
     * @param   GUI
     * @param   createResourceController
     */
    public CreateResourceDialog(GUI gui, CreateResourceController createResourceController)
    {
        super(gui.getMainWindow(), true);
        initComponents();
        
        setGui(gui);
        this.createResourceController = createResourceController;

        // Load resource types
        this.resourceTypes = createResourceController.getResourceTypes();
        for (ResourceTypeInterface r : this.resourceTypes)
            this.cmbType.addItem(r.getId());
    }
    
    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/
    
    private GUI gui;
    
    /**
     * Sets the GUI.
     * 
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }
    
    /**
     * Returns the GUI reference.
     * 
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }
    
    /***************************************************************************
     *                          Controllers
     ***************************************************************************/
    
    private CreateResourceController createResourceController;

    /***************************************************************************
     *                          Resource types
     ***************************************************************************/

    private List<ResourceTypeInterface> resourceTypes;
    
    /***************************************************************************
     *                          Dialog-specific functionality
     ***************************************************************************/

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCreateResource = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();
        lblDescription = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        btnCreateResource = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Resource");

        lblCreateResource.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblCreateResource.setText("Create Resource");
        lblCreateResource.setName("lblCreateResource"); // NOI18N

        lblType.setText("Type");
        lblType.setName("lblType"); // NOI18N

        cmbType.setName("cmbType"); // NOI18N

        lblDescription.setText("Description");
        lblDescription.setName("lblDescription"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        txtDescription.setName("txtDescription"); // NOI18N
        jScrollPane1.setViewportView(txtDescription);

        btnCreateResource.setText("Create Resource");
        btnCreateResource.setName("btnCreateResource"); // NOI18N
        btnCreateResource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateResourceActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCreateResource)
                    .addComponent(lblType)
                    .addComponent(cmbType, 0, 380, Short.MAX_VALUE)
                    .addComponent(lblDescription)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCreateResource, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCreateResource)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescription)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateResource)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Creates a resource.
 * 
 * @param   evt
 */
private void btnCreateResourceActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
        ResourceTypeInterface type = this.resourceTypes.get(this.cmbType.getSelectedIndex());
        createResourceController.createResource(type, (String)this.cmbType.getSelectedItem());
        gui.handleSuccess("Resource created!");
        gui.getMainWindow().update();
        this.dispose();
    } catch (Throwable exception) {
        gui.handleError(exception);
    }
}                                                 

/**
 * Closes the dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreateResource;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCreateResource;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextArea txtDescription;
    // End of variables declaration//GEN-END:variables
}